package com.example.gokuplayalong.bases.baiduapi;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.baiduapi.security.BaiduKey;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.threadpool.CustomThreadPoolExecutor;
import com.example.gokuplayalong.bases.tools.Logger;
import com.kongzue.dialogx.dialogs.PopTip;

import java.util.Objects;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BaiduApi {
    private static String AccessToken = "";
    String AppID = "115974631";
    static String APIKey = "ZYSrJvuQpvOyF77PofFkUaKN";
    static String SecretKey = "";


    public BaiduApi() {

        SecretKey = BaiduKey.getInstance().getKey();
    }

    public static void textCensor(String text, Handler handler) {

        try {

            CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient okHttpClient;
                        FormBody formBody;
                        Request request;
                        Response response;
                        okHttpClient = new OkHttpClient();
                        formBody = new FormBody.Builder().add("grant_type", "client_credentials").add("client_id", APIKey).add("client_secret",  BaiduKey.getInstance().getKey()).build();
                        request = new Request.Builder().post(formBody).url("https://aip.baidubce.com/oauth/2.0/token").build();
                        response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {
                            AccessToken = JSON.parseObject(response.body().string()).getString("access_token");
                        }


                        okHttpClient = new OkHttpClient();
                        formBody = new FormBody.Builder().add("access_token", AccessToken).add("text", text).build();
                        request = new Request.Builder().post(formBody).url("https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined").build();
                        response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {
                            JSONObject jsonObject = JSON.parseObject(response.body().string());
                            Bundle bundle = new Bundle();
                            bundle.putString("response", jsonObject.toString());
                            if (Objects.equals(jsonObject.getString("conclusion"), "合规")) {
                                Message message = new Message();
                                message.what = 1;
                                message.setData(bundle);
                                handler.sendMessage(message);
                            } else {
                                Message message = new Message();
                                message.what = 2;
                                message.setData(bundle);
                                handler.sendMessage(message);
                            }
                        }


                    } catch (Throwable e) {
                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                    }
                }
            });
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    public static void imageEncodedCensor(String imageEncoded, Handler handler) {
        try {
            CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient okHttpClient;
                        FormBody formBody;
                        Request request;
                        Response response;
                        okHttpClient = new OkHttpClient();


                        formBody = new FormBody.Builder().add("grant_type", "client_credentials").add("client_id", APIKey).add("client_secret", BaiduKey.getInstance().getKey()).build();
                        request = new Request.Builder().post(formBody)
                                .addHeader("Content-Type", "application/json")
                                .addHeader("Accept", "application/json")
                                .url("https://aip.baidubce.com/oauth/2.0/token").build();
                        response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {
                            AccessToken = JSON.parseObject(response.body().string()).getString("access_token");
                        } else {
                            PopTip.show("获取accessToken 失败 ,请联系在线客服！");
                        }
//


                        Logger.d(AccessToken);

                        okHttpClient = new OkHttpClient();
                        formBody = new FormBody.Builder().add("access_token", AccessToken).add("image", imageEncoded).build();
                        request = new Request.Builder().post(formBody).url("https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined").build();
                        response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {

                            String string = response.body().string();
                            Logger.d(string);
                            JSONObject jsonObject = JSON.parseObject(string);
                            Bundle bundle = new Bundle();
                            bundle.putString("response", jsonObject.toString());
                            if (Objects.equals(jsonObject.getString("conclusion"), "合规")) {
                                Message message = new Message();
                                message.what = 1;
                                message.setData(bundle);
                                handler.sendMessage(message);
                            } else {
                                Message message = new Message();
                                message.what = 2;
                                message.setData(bundle);
                                handler.sendMessage(message);
                            }
                        }


                    } catch (Throwable e) {
                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                    }
                }
            });
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

}
